<?php

/**
 * @file
 * Yandex.Maps block.
 */


/**
 * Implements hook_block_info().
 */
function yamaps_block_info() {
  $blocks['yamaps'] = array(
    'info' => t('Yandex.Map'),
    'cache' => DRUPAL_NO_CACHE,
  );
  return $blocks;
}

/**
 * Implements hook_block_configure().
 * @see yamaps_field_process()
 */
function yamaps_block_configure($delta = '') {
  $form = array();
  if ($delta == 'yamaps') {
    $form['yamaps_block_controls'] = array(
      '#title' => t('Show controls'),
      '#type' => 'checkbox',
      '#default_value' => variable_get('yamaps_block_controls', TRUE),
    );
    $form['yamaps_block_traffic'] = array(
      '#title' => t('Show traffic'),
      '#type' => 'checkbox',
      '#default_value' => variable_get('yamaps_block_traffic', FALSE),
    );
    $form['yamaps_block_width'] = array(
      '#title' => t('Map width'),
      '#field_suffix' => ' ' . t('in pixels or in percentage'),
      '#type' => 'textfield',
      '#default_value' => variable_get('yamaps_block_width', '168px'),
      '#size' => 5,
      '#element_validate' => array('yamaps_field_validate_pixels_percentage'),
      '#required' => TRUE,
    );
    $form['yamaps_block_height'] = array(
      '#title' => t('Map height'),
      '#field_suffix' => ' ' . t('in pixels or in percentage'),
      '#type' => 'textfield',
      '#default_value' => variable_get('yamaps_block_height', '200px'),
      '#size' => 5,
      '#element_validate' => array('yamaps_field_validate_pixels_percentage'),
      '#required' => TRUE,
    );

    // Add elements from default field edit form.
    $element = array(
      '#delta' => 'block-' . $delta,
      '#required' => FALSE,
      '#value' => array(
        'coords' => variable_get('yamaps_block_coords', NULL),
        'type' => variable_get('yamaps_block_type', 'yandex#map'),
        'placemarks' => variable_get('yamaps_block_placemarks', NULL),
        'lines' => variable_get('yamaps_block_lines', NULL),
        'polygons' => variable_get('yamaps_block_polygons', NULL),
        'routes' => variable_get('yamaps_block_routes', NULL),
      ),
    );
    $form += yamaps_field_process($element, NULL, NULL);
  }
  return $form;
}

/**
 * Implements hook_block_save().
 */
function yamaps_block_save($delta = '', $edit = array()) {
  if ($delta == 'yamaps') {
    variable_set('yamaps_block_controls', $edit['yamaps_block_controls']);
    variable_set('yamaps_block_traffic', $edit['yamaps_block_traffic']);
    variable_set('yamaps_block_width', $edit['yamaps_block_width']);
    variable_set('yamaps_block_height', $edit['yamaps_block_height']);
    variable_set('yamaps_block_coords', $edit['coords']);
    variable_set('yamaps_block_type', $edit['type']);
    variable_set('yamaps_block_placemarks', $edit['placemarks']);
    variable_set('yamaps_block_lines', $edit['lines']);
    variable_set('yamaps_block_polygons', $edit['polygons']);
    variable_set('yamaps_block_routes', $edit['routes']);
  }
}

/**
 * Implements hook_block_view().
 */
function yamaps_block_view($delta = '') {
  $block = array();
  if ($delta == 'yamaps') {
    $block['subject'] = t('Yandex.Map');
    $block['content'] = yamaps_block_content($delta);
  }
  return $block;
}

/**
 * Returns block content.
 * @see yamaps_block_view()
 */
function yamaps_block_content($delta) {
  // Map id.
  $id = 'ymap-block-' . $delta;
  // Map information.
  $coords = drupal_json_decode(variable_get('yamaps_block_coords', NULL));
  $width = variable_get('yamaps_block_width', '168px');
  $height = variable_get('yamaps_block_height', '200px');
  // Map initialization parameters.
  $map = array(
    'init' => array(
      'center' => $coords['center'],
      'zoom' => $coords['zoom'],
      'type' => variable_get('yamaps_block_type', 'yandex#map'),
      'behaviors' => array('scrollZoom', 'dblClickZoom', 'drag'),
    ),
    'controls' => variable_get('yamaps_block_controls', TRUE),
    'traffic' => variable_get('yamaps_block_traffic', FALSE),
    'placemarks' => drupal_json_decode(variable_get('yamaps_block_placemarks', NULL)),
    'lines' => drupal_json_decode(variable_get('yamaps_block_lines', NULL)),
    'polygons' => drupal_json_decode(variable_get('yamaps_block_polygons', NULL)),
    'routes' => drupal_json_decode(variable_get('yamaps_block_routes', NULL)),
    'edit' => FALSE,
  );
  // Adding map to js.
  drupal_add_js(array('yamaps' => array($id => $map)), 'setting');
  // Load library.
  drupal_add_library('yamaps', 'yamaps.full');

  // Return map container div.
  return array(
    '#type' => 'html_tag',
    '#tag' => 'div',
    '#attributes' => array(
      'id' => $id,
      'style' => 'width:' . $width . ';height:' . $height,
    ),
    '#value' => '',
  );
}
